﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using com.subb.WebInformation;
using WebEntities.Models.WebInfo;

namespace WebsiteAdmin.Controllers
{
    [Authorize]
    public class ExpertCataController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllExpertCata(StoreRequestParameters parms)
        {
            WI_ExpertCataDAL mdal = new WI_ExpertCataDAL();
            int ttlCount;
            List<WI_ExpertCata> dbExpertCatalist = mdal.GetAllExpertCata(parms.Start, parms.Limit, out ttlCount);
            return this.Store(dbExpertCatalist, ttlCount);
        }

        public ActionResult GetAllExpertByCata()
        {
            WI_ExpertCataDAL mdal = new WI_ExpertCataDAL();
            List<WI_ExpertCata> ppcataList = mdal.GetAllExpertCata();

            List<ExpertCata> pcatList = new List<ExpertCata>();
            foreach (var ppcata in ppcataList)
            {
                ExpertCata pcat = ExpertCata.AsExpertCata(ppcata);
                pcatList.Add(pcat);
            }
            return this.Store(pcatList);
        }

        [HttpPost]
        public ActionResult DeleteExpertCata(List<ExpertCata> delExpertCata)
        {
            try
            {
                WI_ExpertCataDAL mdal = new WI_ExpertCataDAL();
                foreach (var ExpertCata in delExpertCata)
                {
                    mdal.RemoveExpertCata(ExpertCata.ExpertCataID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewExpertCataWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建目录",
                Width =250,
                Height = 200,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.LayoutAdd,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateExpertCata"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateExpertCata()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateExpertCata(ExpertCata obj)
        {
            //新建菜单
            FormPanelResult response = new FormPanelResult();
            try
            {
                WI_ExpertCataDAL mdal = new WI_ExpertCataDAL();
                WI_ExpertCata addObj = new WI_ExpertCata() { ExpertCataName = obj.ExpertCataName };
                mdal.CreateExpertCata(addObj);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "数据创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        public ActionResult EditExpertCataWindow(int ExpertCataId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "编辑目录",
                Width = 250,
                Height = 200,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.LayoutEdit,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("EditExpertCata"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("ExpertCataId", ExpertCataId.ToString()));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditExpertCata(int ExpertCataId)
        {
            WI_ExpertCataDAL mdal = new WI_ExpertCataDAL();
            WI_ExpertCata dbExpertCata = mdal.GetExpertCata(ExpertCataId);
             ExpertCata ExpertCata = ExpertCata.AsExpertCata(dbExpertCata);
            return View(ExpertCata);
        }

        [HttpPost]
        public ActionResult EditExpertCata(ExpertCata obj)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                WI_ExpertCataDAL mdal = new WI_ExpertCataDAL();
                mdal.UpdateExpertCata(obj.ExpertCataID, obj);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "数据更新成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
